import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class EMQXClient {

    private static final String BROKER = "tcp://211.91.71.43:1883";
    private static final String CLIENT_ID = "java-client-001";

    public static void main(String[] args) {

        try {

            MqttClient client = new MqttClient(BROKER, CLIENT_ID, new MemoryPersistence());

            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setUserName("sanzhu");
            connOpts.setPassword("sanzhu".toCharArray());
            connOpts.setCleanSession(true);

            System.out.println("Connecting to broker: " + BROKER);
            client.connect(connOpts);
            System.out.println("Connected");

            // TODO: 在此处进行消息的订阅和发布操作
            String topic = "sanzhuTopic/1";
            String content = "你好, MQTT!";
            int qos = 1;
            boolean retained = false;
            client.publish(topic, content.getBytes(), qos, retained);

            client.disconnect();
            System.out.println("Disconnected");

        } catch (MqttException e) {
            e.printStackTrace();
        }

    }
}